package com.kaifamiao.io.d0921;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * read() 读取一个字节
 * read(byte[]) 读取 byte数组长度个字节，放入数组中。返回读取到的字节数，如果没有则返回 -1
 * new String(byte[], offset, length) 将 byte 数组中从 offset 索引开始的 length 个字节以默认编码转换成字符串
 * new String(byte[], offset, length, charset) 将 byte 数组中从 offset 索引开始的 length 个字节以指定编码转换成字符串
 */
public class FileInputStreamDemo3 {

    public static void main(String[] args) {
        // 创建对象
        String path = "D:\\io\\file-input.txt";
        try (FileInputStream fileInputStream = new FileInputStream(path);) {

            long size = fileInputStream.available();
            System.out.println("还有 " + size + " 个字节未读取");

            byte[] bytes = fileInputStream.readAllBytes();

            System.out.println(new String(bytes, Charset.forName("gbk")));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
